package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import net.tsz.afinal.json.JsonMap;
import so.shanku.essential.R;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.view.ListViewNoScroll;

import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/19 0019.
 * 购物车的适配器
 */
public class ShopCartAdapter extends MyBaseAdapter implements ShopProductListAdapter.ItemCompountClickListener {
	public ShopCartAdapter(Context context) {
		super(context);
	}

	public ShopCartAdapter(Context context, List<? extends Map<String, ?>> datas) {
		super(context, datas);
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ShopCartAdapterViewHolder shopCartAdapterViewHolder;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.shopcart_item, null);
			shopCartAdapterViewHolder = new ShopCartAdapterViewHolder();
			ViewUtils.inject(shopCartAdapterViewHolder, view);
			view.setTag(shopCartAdapterViewHolder);
		} else {
			shopCartAdapterViewHolder = (ShopCartAdapterViewHolder) view.getTag();
		}
		setTags(i,shopCartAdapterViewHolder);
		bindDatas(i, shopCartAdapterViewHolder);
		return view;
	}

	private void setTags(int i, ShopCartAdapterViewHolder shopCartAdapterViewHolder) {
		shopCartAdapterViewHolder.select_goods_cb.setTag(i);
		shopCartAdapterViewHolder.lv_noScrool.setTag(i);
		shopCartAdapterViewHolder.brandName_tv.setTag(i);
	}

	private void bindDatas(int i, ShopCartAdapterViewHolder shopCartAdapterViewHolder) {
		JsonMap jsonMap = (JsonMap) datas.get(i);
		shopCartAdapterViewHolder.select_goods_cb.setSelected(jsonMap.getBoolean("isShopChecked"));
		shopCartAdapterViewHolder.brandName_tv.setText(jsonMap.getStringNoNull("shopName"));
		shopCartAdapterViewHolder.brand_totle_price.setText(StringUtil.getFormatMoneyWithSign(jsonMap.getStringNoNull("orderPrice")));
		ShopProductListAdapter goodsListAdapter = new ShopProductListAdapter(context);
		goodsListAdapter.setParentPosition(i);
		goodsListAdapter.setItemCompountClickListener(this);
		goodsListAdapter.setDatas(jsonMap.getList_JsonMap("shoppingCartList"));
		shopCartAdapterViewHolder.lv_noScrool.setAdapter(goodsListAdapter);
	}

	@Override
	public void itemCompountClick(int itemPosition, Constant.ShopCartItemCompontType type, int childIndex) {
		if(itemClickListener!=null){
			itemClickListener.onItemClick(itemPosition, type, childIndex);
		}
	}

	private class ShopCartAdapterViewHolder {
		@ViewInject(R.id.select_goods_cb)
		ImageView select_goods_cb;

		/**
		 * 点选店铺的
		 * @param view
		 */
		@OnClick(R.id.select_goods_cb)
		public void select_goods_cb(View view) {
		   if(itemClickListener!=null){
			   itemClickListener.onItemClick((Integer) view.getTag(),Constant.ShopCartItemCompontType.SELECT_GOODS_BRAND,-1);
		   }
		}

		@ViewInject(R.id.brandName_tv)
		TextView brandName_tv;
		 @OnClick(R.id.brandName_tv)
		 public void brandName_tv_click(View view){
			 if(itemClickListener!=null){
				 itemClickListener.onItemClick((Integer) lv_noScrool.getTag(), Constant.ShopCartItemCompontType.CLICK_BRAND_NAME,-1);
			 }
		 }
		@ViewInject(R.id.lv_noScrool)
		ListViewNoScroll lv_noScrool;
		 @OnItemClick(R.id.lv_noScrool)
		public void lv_noScrool_item_click(AdapterView<?> adapterView, View view, int i, long l){
			 if(itemClickListener!=null){
				itemClickListener.onItemClick((Integer) lv_noScrool.getTag(), Constant.ShopCartItemCompontType.CLICK_GOODS_ITEM,i);
			 }
		 }
		@ViewInject(R.id.brand_totle_price)
		TextView brand_totle_price;
	}


	private OnItemClickListener itemClickListener;
	public interface OnItemClickListener {
		/**
		 *
		 * @param position 根位置
		 * @param shopCartitemType  类别
		 * @param index  点击具体的商品位置
		 */
		void onItemClick(int position,Constant.ShopCartItemCompontType shopCartitemType,int index);
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.itemClickListener = listener;
	}
}
